let files = require.context('@/pages', true, /\.vue$/)
let repeatRoutes = files.keys().map(path => {

    let regExp = /(\/index\.vue|\.vue)$/

    let currentComponent = files(path).default;

    let { meta, name, redirect } = currentComponent;

    path = path.substring(1).replace(regExp, '')

    return {
        path: path ? path : '/pages',
        meta: meta ?? {},
        name: name ?? path.substring(1).split('/').join('-').toLowerCase(),
        redirect: redirect ?? null,
        component: () => import("@/pages" + path)
    }
})


function convertTree(repeatRoutes) {
    repeatRoutes = [...repeatRoutes] 
    let treeRoutes = [];
    repeatRoutes.forEach(route => {

        let routePathArr = route.path.substring(1).split('/')

        route.id = routePathArr.join('-')

        if (routePathArr.length > 1) {

            let pathArr = [...routePathArr];
            pathArr.pop()
            let pid = pathArr.join("-")
            route.pid = pid;
        }
    })

    repeatRoutes.forEach(route => {
        route.path = route.path.toLowerCase()
        if (route.path === "/page404") {
            route.path = "*"
        }
        let parent = repeatRoutes.find(v => route.pid === v.id)
        if (parent) {
            if (!parent.children) {
                parent.children = []
            }
            route.path = route.path.substring(1).split("/").pop()
            parent.children.push(route)
        } else {
            treeRoutes.push(route)
        }
    })
    return treeRoutes
}

let res = convertTree(repeatRoutes)
console.log(res)
export default [...res]